
package com.wyzz.hr.bo.impl;

import com.opensymphony.xwork2.ActionContext;
import com.wyzz.hr.base.Constants;
import com.wyzz.hr.base.UsersAuthority;
import com.wyzz.hr.bo.IHasAuthBO;

import java.util.*;
import javax.servlet.jsp.PageContext;
import org.directwebremoting.WebContextFactory;

public class HasAuthBOImpl implements IHasAuthBO, Constants {

	public HasAuthBOImpl() {
	}

	private synchronized HashMap initAction() throws Exception {
		return (HashMap) ActionContext.getContext().getSession()
				.get("authList");
	}

	private synchronized HashMap initDWR() throws Exception {
		return (HashMap) WebContextFactory.get().getSession().getAttribute(
				"authList");
	}

	private synchronized HashMap initJSP(PageContext pageContext)
			throws Exception {
		return (HashMap) pageContext.getSession().getAttribute("authList");
	}

	public boolean hasActionAuth(UsersAuthority inputUsersAuthority)
			throws Exception {
		HashMap usrAuthList = initAction();
		return usrAuthList != null ? usrAuthList
				.containsKey(inputUsersAuthority) : false;
	}

	public boolean hasActionAuth(List inputUsersAuthoritySet) throws Exception {
		HashMap usrAuthList = initAction();
		if (usrAuthList == null)
			return false;
		for (Iterator inputUsersAuthorityIterator = inputUsersAuthoritySet
				.iterator(); inputUsersAuthorityIterator.hasNext();)
			if (usrAuthList.containsKey(inputUsersAuthorityIterator.next()))
				return true;

		return false;
	}

	public boolean hasActionAuth(String inputPrefix) throws Exception {
		HashMap usrAuthList = initAction();
		return usrAuthList != null ? usrAuthList.containsValue(inputPrefix)
				: false;
	}

	public boolean hasDWRAuth(UsersAuthority inputUsersAuthority)
			throws Exception {
		HashMap usrAuthList = initDWR();
		return usrAuthList != null ? usrAuthList
				.containsKey(inputUsersAuthority) : false;
	}

	public boolean hasDWRAuth(List inputUsersAuthoritySet) throws Exception {
		HashMap usrAuthList = initDWR();
		if (usrAuthList == null)
			return false;
		for (Iterator inputUsersAuthorityIterator = inputUsersAuthoritySet
				.iterator(); inputUsersAuthorityIterator.hasNext();)
			if (usrAuthList.containsKey(inputUsersAuthorityIterator.next()))
				return true;

		return false;
	}

	public boolean hasDWRAuth(String inputPrefix) throws Exception {
		HashMap usrAuthList = initDWR();
		return usrAuthList != null ? usrAuthList.containsValue(inputPrefix)
				: false;
	}

	public boolean hasJSPAuth(PageContext pageContext,
			UsersAuthority inputUsersAuthority) throws Exception {
		HashMap usrAuthList = initJSP(pageContext);
		return usrAuthList != null ? usrAuthList
				.containsKey(inputUsersAuthority) : false;
	}

	public boolean hasJSPAuth(PageContext pageContext,
			List inputUsersAuthoritySet) throws Exception {
		HashMap usrAuthList = initJSP(pageContext);
		if (usrAuthList == null)
			return false;
		for (Iterator inputUsersAuthorityIterator = inputUsersAuthoritySet
				.iterator(); inputUsersAuthorityIterator.hasNext();)
			if (usrAuthList.containsKey(inputUsersAuthorityIterator.next()))
				return true;
		return false;
	}

	public boolean hasJSPAuth(PageContext pageContext, String inputPrefix)
			throws Exception {
		HashMap usrAuthList = initJSP(pageContext);
		return usrAuthList != null ? usrAuthList.containsValue(inputPrefix)
				: false;
	}
}
